﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Student
{
    static class SqlDataUtility
    {
        public static DataTable IniteGridData()
        {
            DataRow datarow;
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("姓名");
            dataTable.Columns.Add("学号");
            dataTable.Columns.Add("班级");
            dataTable.Columns.Add("学院");
            dataTable.Columns.Add("备注");

            SqlDataReader dataReader = QueryUtility.ExecuteReader("SELECT Name, NameID, Class, College, Memo  FROM  tbl_Class");
            while (dataReader.Read())
            {
                datarow = dataTable.NewRow();
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    datarow[i] = dataReader[i].ToString();
                }
                dataTable.Rows.Add(datarow);
            }
            dataReader.Close();
            return dataTable;
        }

        public static bool Add(string name, string number, string @class, string college, string memo)
        {

            string sql = string.Format(
                "INSERT INTO tbl_Class (Name, NameID, Class, College, Memo) VALUES('{0}','{1}','{2}','{3}','{4}')", name, number, @class, college, memo);
            return QueryUtility.ExecuteInsert(sql);
        }

        public static bool Save(string name, string number, string @class, string college, string memo)
        {
            string sql = string.Format(
                "UPDATE  tbl_Class  SET  Name = '{1}', Class = '{2}', College = '{3}', Memo = '{4}' where NameID = '{0}'", name, number, @class, college, memo);
            return QueryUtility.ExecuteSql(sql);
        }

        public static bool Delete(string number)
        {
            string sql = string.Format("DELETE FROM tbl_Class where NameID = '{0}'", number);
            return QueryUtility.ExecuteSql(sql);
        }

    }
}
